<?php 
	include './header.php';
	$uid=$_SESSION['user']['id'];

	$model=new Model('order_list');

	//每页条数
	$num=1;
	//总条数
	$sql_t="select count(*) from `order_list` where `uid`=$uid";
	$total=$model->find($sql_t)[0]['count(*)'];
	//var_dump($total);
	//总页数
	$pages=ceil($total/$num);
	//获取当前页
	$p=$_GET['p']?$_GET['p']:1;

	$p=min($p,$pages);
	$p=max($p,1);

	$prev=$p-1;
	$next=$p+1;

	$start=($p-1)*$num;
	$offset=$num;

	$sql="select * from `order_list` where `uid`=$uid order by ordertime desc limit $start,$offset";
	$list=$model->find($sql);
	if (!empty($list)) 
	{
		foreach ($list as $k => $v) 
		{
			$sql1="select
						g.id      		gid,
						g.price  		gp,
						gi.name   		giname,
						g.description   gd,
						oi.num 			ion
					from
						goods           g,
						goods_img		gi,
						order_list		ol,
						order_info		oi
					where
						g.id=oi.goods_id
					and
						g.id=gi.goods_id
					and
						ol.id=oi.order_id
					and
						ol.id=".$v['id']."
					
					limit 0,2
				";
				$list_info=$model->find($sql1);
				$list[$k]['son']=$list_info;
		}
	}
	$str="
	<a href='".URL."/personal_order.php' style=color:#ED145B;>首页</a>
	<a href='".URL."/personal_order.php?p=$prev' style=color:#ED145B;>上一页</a>
	<a href='".URL."/personal_order.php?p=$p' style=color:#ED145B;>$p</a>
	<a href='".URL."/personal_order.php?p=$next' style=color:#ED145B;>下一页</a>
	<a href='".URL."/personal_order.php?p=$pages' style=color:#ED145B;>尾页</a>";
?>
<div class="clear"></div>
<div id="main">
	<div class="goods">
		<div class="person_left">
			<ul>
				<a href="<?php echo URL;?>/personal_info.php"><li><span style="line-height:100px;">个人信息</span></li></a>
				<a href="<?php echo URL;?>/personal_modify.php"><li><span  style="line-height:100px;">修改密码</span></li></a>
				<a href="<?php echo URL;?>/personal_order.php"><li><span  style="line-height:100px;">查看订单</span></li></a>
			</ul>
		</div>
		<div class="person_right">
			<div class="person_main">
				<!--遍历 用户订单-->
				<h2 style="display:block;width:100px;height:50px;margin-left:40px;margin-top:20px;font:bold 24px/30px 宋体;">我的订单</h2>
				<table cellpadding="5" cellspacing="0" align="center" width="800" style="margin-left:40px;border:1px dashed #ccc;">
					<tr style="height:50px;">
						<th>订单编号</th>
						<th>订单状态</th>
						<th>是否付款</th>
						<th>下单时间</th>
						<th>操作</th>
					</tr>
					<?php if (!empty($list)) :?>
						<?php foreach ($list as $key => $value) :?>
							<tr style="height:30px;background:#ED145B;">
								<th><?php echo $value['ordernumber'];?></th>
								<th>
									<?php 
										switch ($value['is_delivery']) 
										{
											case 0:
												echo '未发货';
												break;
											case 1:
												echo '<a href="'.URL.'/dopersonal.php?a=shou&id='.$value['id'].'">等待收货';
												break;
											case 2:
												echo '已签收';
												break;
										}
									?>
								</th>
								<th>
									<?php echo $value['is_pay']?'已付款':'<a href="'.URL.'/dopersonal.php?a=pay&id='.$value['id'].'">付款</a>';?>
								</th>
								<th><?php echo date('Y年m月d日',$value['ordertime']);?></th>
								<th><a href="<?php echo URL;?>/comments.php?id=<?php echo $value['id'];?>">查看详情</a></th>
							</tr>
							<tr>
								<th colspan="5">
									<?php foreach ($value['son'] as $ke => $val) :?>
										<ul>
										<li><?php echo $val['gid'];?></li>
										<li><?php echo $val['gp'];?></li>
										<li>
											<img src="<?php echo URL.'/public/uploads/'.substr($val['giname'], 0,4).'/'.substr($val['giname'], 4,2).'/'.substr($val['giname'], 6,2).'/50_'.$val['giname'];?>"
											alt="美工回家生娃了">
										</li>
										<li style="width:400px;"><?php echo $val['gd'];?></li>
										<li><?php echo $val['ion'];?></li>
										<li><?php echo $val['ion']*$val['gp'];?></li>
										</ul>
									<?php endforeach;?>
								</th>
							</tr>
						<?php endforeach;?>
					<?php else:?>
						<tr>
							<th><a href="">您还没有订单，赶快购物去吧</a></th>
						</tr>
					<?php endif;?>
					<tr style="height:80px">
						<th colspan="5" align="center"><?php echo $str;?></th>
					</tr>
				</table>
			</div> 
		</div>
	</div>
</div>
<div class="clear"></div>
<?php 
	include PATH.'/footer.php';
?>